﻿using DiffuseEnjoyIdle.TimedTaskCentre.Hangfire;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

namespace DiffuseEnjoyIdle.Extension.Extensions.Hangfire
{
    public static class HangfireJobSetup
    {
        public static IServiceCollection AddHangfireInstall(this IServiceCollection services, string hangfireConn)
        {
            services.AddHangfire(hangfireConn);
            services.AddSingleton<HangfireManager>();

            return services;
        }

        public static IApplicationBuilder UseJobs(this IApplicationBuilder app, IServiceProvider serviceProvider)
        {
            using var scope = serviceProvider.CreateScope();
            var jobManager = scope.ServiceProvider.GetRequiredService<HangfireManager>();

            jobManager.ExecuteAsync().GetAwaiter();

            return app;
        }
    }
}
